VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Monitor"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit

Private Const WM_DISPLAYCHANGE = &H7E
Private Const HWND_BROADCAST = &HFFFF&
Private Const CCDEVICENAME = 32
Private Const CCFORMNAME = 32
Private Const DM_BITSPERPEL = &H40000
Private Const DM_PELSWIDTH = &H80000
Private Const DM_PELSHEIGHT = &H100000
Private Const CDS_UPDATEREGISTRY = &H1
Private Const CDS_TEST = &H4
Private Const DISP_CHANGE_SUCCESSFUL = 0
Private Const DISP_CHANGE_RESTART = 1
Private Const BITSPIXEL = 12

Private Type DEVMODE
    dmDeviceName As String * CCDEVICENAME
    dmSpecVersion As Integer
    dmDriverVersion As Integer
    dmSize As Integer
    dmDriverExtra As Integer
    dmFields As Long
    dmOrientation As Integer
    dmPaperSize As Integer
    dmPaperLength As Integer
    dmPaperWidth As Integer
    dmScale As Integer
    dmCopies As Integer
    dmDefaultSource As Integer
    dmPrintQuality As Integer
    dmColor As Integer
    dmDuplex As Integer
    dmYResolution As Integer
    dmTTOption As Integer
    dmCollate As Integer
    dmFormName As String * CCFORMNAME
    dmUnusedPadding As Integer
    dmBitsPerPel As Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
End Type

Private Declare Function EnumDisplaySettings Lib "user32" Alias "EnumDisplaySettingsA" (ByVal lpszDeviceName As Long, ByVal iModeNum As Long, lpDevMode As Any) As Boolean
Private Declare Function ChangeDisplaySettings Lib "user32" Alias "ChangeDisplaySettingsA" (lpDevMode As Any, ByVal dwFlags As Long) As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As Any) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function CreateDCAsNull Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, lpDeviceName As Any, lpOutput As Any, lpInitData As Any) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long

Public Function XResolution() As Long
XResolution = Screen.Width / Screen.TwipsPerPixelX
End Function

Public Function YResolution() As Long
YResolution = Screen.Height / Screen.TwipsPerPixelY
End Function

Public Function ColorDepth() As Long
Dim lR As Long
Dim lHDC As Long
lHDC = CreateDCAsNull("DISPLAY", ByVal 0&, ByVal 0&, ByVal 0&)
lR = GetDeviceCaps(lHDC, BITSPIXEL)
DeleteDC lHDC
ColorDepth = lR
End Function

Public Sub ChangeResolution(ByVal X As Long, ByVal Y As Long, ByVal BPP As Long)
Dim tDevMode As DEVMODE
Dim lScreenInfo As Long
Dim lResult As Long
Dim lDC As Long

lDC = CreateDC("DISPLAY", vbNullString, vbNullString, ByVal 0&)
lResult = EnumDisplaySettings(0&, 0&, tDevMode)

tDevMode.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT Or DM_BITSPERPEL
tDevMode.dmPelsWidth = X
tDevMode.dmPelsHeight = Y
tDevMode.dmBitsPerPel = BPP

lResult = ChangeDisplaySettings(tDevMode, CDS_TEST)

Select Case lResult
    Case DISP_CHANGE_RESTART
        MsgBox "Windows needs to restart before the changes will take effect.", vbInformation, "Change Screen Resolution"
    Case DISP_CHANGE_SUCCESSFUL
        lResult = ChangeDisplaySettings(tDevMode, CDS_UPDATEREGISTRY)
        lScreenInfo = Y * 2 ^ 16 + X
        SendMessage HWND_BROADCAST, WM_DISPLAYCHANGE, ByVal BPP, ByVal lScreenInfo
    Case Else
        MsgBox "Resolution is not supported.", vbCritical, "Error"
End Select

DeleteDC lDC
End Sub

